#!/usr/bin/perl
#dealprint.cgi
#deal with the send button

require './printer-lib.pl';
&init_config();
&ReadParse();

#&header($text{'set_title'}, "", "index", 0, 1,undef,undef);

my @localusers;
my @localadmins;
my $pagestat=$in{'pagestate'};


if ($in{'selectone'} =~ /1/){
	&check_printers;
	&check_printcap;
	&redirect("index.cgi");
}elsif($in{'selectone'}=~ /2/){
	if ($in{'printer'} eq ''){
		&error($text{'noprinter'});
	}else{
		if (&check_valid($in{'printer'}) ne 'yes'){
			&error($text{'notsmb'});
		}else{
			&header($text{'set_title'}, "", "config", 0, 1);
			print "<form name=cfgform action=saveprinter.cgi method=post onsubmit='document.cfgform.savelp.disabled = true;'>";	

			$mod = &get_lpmod($in{'printer'});
			$mod = $in{'printer'};
			$smbname= &get_lpsmbname($in{'printer'});
			print "<input type=hidden name=smbname value=\'$smbname\'>";
			if($pagestat eq 'new'){			
				$in{'localusers'} = "";
				$in{'localadmins'} = "";
				$in{'public'} = "no";
				$in{'usertyperdn'} = "0";
				$in{'sharename'} = $smbname;
				
				#@alluser= &get_alluser;
				if ($smbname eq ''){
					$in{'public'} ='no';
					$in{'localadmins'}='';
					$in{'localusers'}='';
					$in{'cancle'}='yes';
				}else{
					@smbinfo=&get_smbinfo($smbname);
					$in{'public'} = pop @smbinfo;
					while(@smbinfo>0)
					{	
						$user=pop @smbinfo;
						if($user eq "-"){
							last;
						}
						push (@localadmins,$user);
					}
					while(@smbinfo>0)
					{	
						$user=pop @smbinfo;
						if($user eq "--"){
							last;
						}
						push (@localusers,$user);
					}
					
					
					$in{'localadmins'} = join (":",@localadmins);
					$in{'localusers'} = join (":",@localusers);
					@localadmins= undef;
					@localusers=undef;	
					$in{'cancle'}='no';						
				}
			}
			
			@localusers = split(/:/,$in{'localusers'});
			@localadmins = split(/:/,$in{'localadmins'});
			$public = $in{'public'};
					
			printf "<input type=hidden name=selectone value=%s>\n",$in{'selectone'};
			printf "<input type=hidden name=printer value=%s>\n",$in{'printer'};
			print "<input type=hidden name=pagestate value='old'>\n";			
			printf "<input type=hidden name=localusers value=\'%s\'>\n",$in{'localusers'};
			printf "<input type=hidden name=localadmins value=\'%s\'>\n",$in{'localadmins'};
			
			print "<table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
			
			print "<tr><td><table class=mode1 border=0  cellspacing=0 cellpadding=4>\n";
			print "<tr><td><b>$text{'type'}</b></td><td>$mod</td>\n";
			print "</table></td></tr>";
			print "<tr><td colspan=2 width=600><hr></td></tr>";			
			
			print "<tr><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";			
			print "<tr><td><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
			printf "<tr><td><b>$text{'name'}</b></td><td><input %s name=sharename value=$in{'sharename'}></td></tr>\n",($in{'cancle'} eq 'yes')? "disabled":"";			
			print "<tr><td><b>$text{'admin'}</b></td>\n";
			print "<td><table border=0 class=mode1>";

			printf "<tr><select %s class=list name=localadminlist size=15>\n",($in{'cancle'} eq 'yes')? "disabled":"";
			
			foreach(@localadmins)
			{
				printf "<option value=\'%s\'>%s\n",$_,$_;
			}
			
			print "</select></tr>\n";
			print "</table></td>\n";
			print "<td><table cellpadding=8>\n";
			printf "<tr><td><input %s class=button1 type=button name=add1 onclick='addadmin();' value=\'$text{'add'}\'></td></tr>\n",($in{'cancle'} eq 'yes')? "disabled":"";
			printf "<tr><td><input %s class=button1 type=button name=rm1 onclick='rmadmin();' value=\'$text{'rm'}\'></td></tr>\n",($in{'cancle'} eq 'yes')? "disabled":"";
			print "<input name=listuser type=hidden value = ''>";
			print "</table>\n";
			print "</td></tr>\n";
		
			print "<tr><td><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
			#print "<tr><td><b>$text{'user'}</b></td>\n";
			#printf "<tr><td><input %s type=checkbox name=public value='yes' onclick ='return disable_public();'><b>$text{'public'}</b></td></tr>\n",,($in{'cancle'} eq 'yes')? "disabled":(($public eq "yes")? "checked":"");
			#print "</table></td>";
			#print "<td><table>\n";
			#printf "<select %s class=list name=localuserlist size=8>\n", ($in{'cancle'} eq 'yes')? "disabled":(($public eq "yes")? "disabled":"");			
			#foreach(@localusers)
			#{	
			#	printf "<option value=%s>%s\n",$_,$_;
			#}
			#print "</select></table></td>\n";
			#print "<td><table>";
			#printf "<tr><td><input %s class=sbutton type=button name=add2 onclick='adduser();' value=\'$text{'add'}\'></td></tr>\n",,($in{'cancle'} eq 'yes')? "disabled":(($public eq "yes")? "disabled":"");
			#printf "<tr><td><input %s class=sbutton type=button name=rm2 onclick='rmuser();' value=\'$text{'rm'}\'></td></tr>\n",,($in{'cancle'} eq 'yes')? "disabled":(($public eq "yes")? "disabled":"");
			print "<input name=listadmin type=hidden value = ''>";
			print "</table></td></tr></table></td>\n";		

			print "<td>";
			print "<table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
			print "<tr><b>$text{'alluser'}</b></tr>";
			printf "<tr><select %s name=usertyperdn onchange='change_user_list();'>\n",($in{'cancle'} eq 'yes')? "disabled":"";
			printf "<option %s value='0'>$text{'show_localuser'}\n",($in{'usertyperdn'} eq "0")?"selected":"";
			printf "<option %s value='1'>$text{'show_localgroup'}\n", ($in{'usertyperdn'} eq "1")?"selected":"";
			printf "<option %s value='2'>$text{'show_pdcuser'}\n", ($in{'usertyperdn'} eq "2")?"selected":"";
			printf "<option %s value='3'>$text{'show_pdcgroup'}\n", ($in{'usertyperdn'} eq "3")?"selected":"";
			printf "</select></tr>\n";
	
			printf "<tr><select %s name=userslist size=15 class=list>\n",($in{'cancle'} eq 'yes')? "disabled":"";
			if ($in{'usertyperdn'} eq "0")
			{
				open(LOCALUSER, $config{'localuser_file'});
				while ($line = <LOCALUSER>) {
					my @users = split(/:/, $line);
					print "<option value='$users[0]'>$users[0]\n";
				}
				close(LOCALUSER);
			}

			if ($in{'usertyperdn'} eq "1")
			{
				open(LOCALGROUP, $config{'localgroup_file'});
				while ($line = <LOCALGROUP>) {
					$line =~ s/\r|\n//g;
					$line =~ s/ *$//;
					$line =~ s/^ *//;
					$line = '@'.$line;
					print "<option value='$line'>$line\n";
				}
				close(LOCALGROUP);
			}

			if ($in{'usertyperdn'} eq "2")
			{
				open(PDCUSER, $config{'pdcuser_file'});
				while ($line = <PDCUSER>) {
					$line =~ s/\r|\n//g;
					$line =~ s/ *$//;
					$line =~ s/^ *//;
					if ($line =~ / /){
						$line =~ s/\\/\\"/;
						$line = $line.'"';
					}
					print "<option value='$line'>$line\n";
				}
				close(PDCUSER);
			}
	
			if ($in{'usertyperdn'} eq "3")
			{
				open(PDCGROUP, $config{'pdcgroup_file'});
				while ($line = <PDCGROUP>) {
					$line =~ s/\r|\n//g;
					$line =~ s/^ *//;
					$line =~ s/ *$//;
					if ($line =~ / /){
						$line =~ s/\\/\\"/;
						$line = $line.'"';
					}					
					$line = '@'.$line;					
					print "<option value='$line'>$line\n";
				}
				close(PDCGROUP);
			}
			print "</select></tr>\n";
			print "</talbe></tr></td></table></tr>\n";
			print "<tr><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
			print "<tr><td>$text{'payattention'}</td></tr>\n";
			print "</talbe></tr>\n";			
			print "<tr><td colspan=2 width=600><hr></td></tr>";			
			printf "<td align=left><input %s type=checkbox name=cancle value='yes' onclick ='return disable_share();'><b>$text{'canselshare'}</b></td></tr>\n",,($in{'cancle'} eq 'yes')? "checked":"";
			print "<tr><table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
			print "<tr> <td align=left><input type=submit class=button1 name=savelp value='$text{'submin'}'></td></tr>\n";
			print "</table></tr></table></form>\n";
			
print <<EOF;
			<script>
			function disable_share()
			{
				if (document.cfgform.cancle.checked){
					document.cfgform.sharename.disabled = true;
					//document.cfgform.localuserlist.disabled = true;
					document.cfgform.localadminlist.disabled = true;
					document.cfgform.userslist.disabled = true;
					document.cfgform.usertyperdn.disabled = true;
					document.cfgform.add1.disabled = true;
					document.cfgform.rm1.disabled = true;
					//document.cfgform.add2.disabled = true;
					//document.cfgform.rm2.disabled = true;
					//document.cfgform.public.disabled = true;	
				}else{
					document.cfgform.sharename.disabled = false;					
					document.cfgform.localadminlist.disabled = false;
					document.cfgform.userslist.disabled = false;				
					document.cfgform.usertyperdn.disabled = false;
					document.cfgform.add1.disabled = false;
					document.cfgform.rm1.disabled = false;
					//document.cfgform.public.disabled = false;
					//if (document.cfgform.public.checked == false){					
					//	document.cfgform.localuserlist.disabled = false;
					//	document.cfgform.add2.disabled = false;
					//	document.cfgform.rm2.disabled = false;					
					//}
				}
			}
			
			//function disable_public(){
			//	if (document.cfgform.public.checked){
			//		document.cfgform.localuserlist.disabled = true;
			//		document.cfgform.add2.disabled = true;
			//		document.cfgform.rm2.disabled = true;
			//		document.cfgform.public.value = "yes";
			//	}else{
			//		document.cfgform.localuserlist.disabled = false;
			//		document.cfgform.add2.disabled = false;
			//		document.cfgform.rm2.disabled = false;	
			//		document.cfgform.public.value = "no";		
			//	}				
			//}
			
			function change_user_list(){
				RefreshPage();
			}
		
			function RefreshPage()
			{
				document.cfgform.action = 'setprinter.cgi';
				document.cfgform.submit();
			}
			
			function addadmin()
			{
				var idx = document.cfgform.userslist.selectedIndex;
        			var i;
        			var find=0;
        			var uitem;

	      			find = 0;
				if (idx < 0){
					alert("$text{'noselectuser'}");
					return;
				}

				for (i = 0; i < document.cfgform.localadminlist.length; i++){
					if(document.cfgform.userslist.options[idx].text == document.cfgform.localadminlist[i].value){
			                 	find = 1;
						break;
					}
				}

        			if(find == 1){
                			alert("$text{'item_exist'}");
                			return false;
	      			}

				if (document.cfgform.localadminlist.length == 0){
					document.cfgform.localadmins.value +=(document.cfgform.userslist.options[idx].text);
				}else{
					document.cfgform.localadmins.value +=(":" + document.cfgform.userslist.options[idx].text);
				}

				RefreshPage();
			}			

			function rmadmin()
			{
				var idx = document.cfgform.localadminlist.selectedIndex;
        			var i;
        			var count=0;
        			var uitem;
        			
				if (idx < 0){
					alert("$text{'noselectuser'}");
					return;
				}
        			
        			document.cfgform.localadmins.value = "";
				for (i = 0; i < document.cfgform.localadminlist.length; i++){			
					if(document.cfgform.localadminlist.options[idx].text != document.cfgform.localadminlist[i].value){
						if(i == (document.cfgform.localadminlist.length - 1) ){
							document.cfgform.localadmins.value +=(document.cfgform.localadminlist[i].text);
						}else{
							if (count == (document.cfgform.localadminlist.length - 2)){
								document.cfgform.localadmins.value +=(document.cfgform.localadminlist[i].text);
							}else{
								document.cfgform.localadmins.value +=(document.cfgform.localadminlist[i].text + ":");
							}
						}
						count++;
					}
					
				}	
				RefreshPage();	
			}

			//function adduser()
			//{
			//	var idx = document.cfgform.userslist.selectedIndex;
        		//	var i;
        		//	var find=0;
        		//	var uitem;

	     		//	find = 0;
			//	if (idx < 0){
			//		alert("$text{'noselectuser'}");
			//		return;
			//	}

			//	for (i = 0; i < document.cfgform.localuserlist.length; i++){
			//		if(document.cfgform.userslist.options[idx].text == document.cfgform.localuserlist[i].value){
			//                	find = 1;
			//			break;
			//		}
			//	}

        		//	if(find == 1){
                	//		alert("$text{'item_exist'}");
                	//		return false;
	      		//	}
				
			//	if (document.cfgform.localuserlist.length == 0){
			//		document.cfgform.localusers.value +=(document.cfgform.userslist.options[idx].text);
			//	}else{
			//		document.cfgform.localusers.value +=(":" + document.cfgform.userslist.options[idx].text);
			
			//	}
			//	RefreshPage();
			//}
			
			//function rmuser()
			//{
			//	var idx = document.cfgform.localuserlist.selectedIndex;
        		//	var i;
        		//	var count=0;
        		//	var uitem;
        		//	
			//	if (idx < 0){
			//		alert("$text{'noselectuser'}");
			//		return;
			//	}
        			
        		//	document.cfgform.localusers.value = "";
			//	for (i = 0; i < document.cfgform.localuserlist.length; i++){		
			//		if(document.cfgform.localuserlist.options[idx].text != document.cfgform.localuserlist[i].value){
			//			if(i == (document.cfgform.localuserlist.length - 1) ){
			//				document.cfgform.localusers.value +=(document.cfgform.localuserlist[i].text);
			//			}else{
			//				if (count ==(document.cfgform.localuserlist.length - 2)){
			//					document.cfgform.localusers.value +=(document.cfgform.localuserlist[i].text);
			//				}else{
			//					document.cfgform.localusers.value +=(document.cfgform.localuserlist[i].text + ":");
			//				}
			//			}
			//			count++;
			//		}					
			//	}	
			//	RefreshPage();	
			//}
			
			</script>
EOF
			
			&footer("index.cgi", $text{'index_title'});
			1;		
		}
	}
}elsif($in{'selectone'}=~ /3/){
	if ($in{'printer'} eq ''){
		&error($text{'noprinter'});
	}else{
		@cm_smblpr = &get_smb_lpr();
		$cmfound = 0;
		foreach (@cm_smblpr){
			if($_ eq $in{'printer'}){
				$cmfound = 1;
				last;
			}
		}
		#if (&check_valid($in{'printer'}) eq 'yes'){
		if ($cmfound) {
			&error($text{'err_has_share'});
		}else{
			#&del_smbprinter($in{'printer'});
			#&del_capprinter($in{'printer'});
			$out = `lpadmin -x $in{'printer'}`;
			if($?) {
				&error($text{'err_failed1'});
			}	
		}
	}
	&redirect("index.cgi");
}else{
	&error("error!");
}

#&footer("index.cgi", $text{'index_title'});
#1;
